package com.share.lottery.activity.caiborecommond;

import java.util.ArrayList;
import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.share.lottery.R;
import com.share.lottery.adapter.CommendRunedAdapter;
import com.share.lottery.base.BaseFragment;
import com.share.lottery.base.Constants;
import com.share.lottery.model.BaseEntity;
import com.share.lottery.model.CaiboRmdBean;


/**
 * @author py
 * 已经完成
 * 
 *
 */
public class CommondedFragment extends BaseFragment {
	private PullToRefreshListView pullToRefreshListView = null;
	private View view;
	private  static CommendRunedAdapter adapter;
	private  static List<CaiboRmdBean> datas = new ArrayList<CaiboRmdBean>();
	private   static List<CaiboRmdBean> newdatas = new ArrayList<CaiboRmdBean>();
	private String lastID = "0";
	private boolean isxl = false;
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view = inflater.inflate(R.layout.cairun_fragment,container,false);
		datas.clear();
		newdatas.clear();
		isxl = false;
		showProgreessDialog(getActivity());
		init();
		return view;
	}
	public void findView() {
		pullToRefreshListView = (PullToRefreshListView) view.findViewById(R.id.audio_list);
		pullToRefreshListView.setMode(Mode.BOTH);
		pullToRefreshListView.setOnRefreshListener(new MyOnRefreshListener2(
				pullToRefreshListView));

	}


	public void setData() {
		if (adapter == null) {
			adapter = new CommendRunedAdapter(getActivity());
		}
		pullToRefreshListView.setAdapter(adapter);

	}


	public void getData() {
		
	//	CaiboRmdController.getInstance().caibaoRmdFinished(handler, 0+"", lastID);

	}

	@Override
	public void setOnClicker() {
		pullToRefreshListView.setOnItemClickListener(new OnItemClickListener() {


			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				CaiboRmdBean caiboRmdBean = (CaiboRmdBean) arg0.getAdapter().getItem(arg2);
				Intent intent = new Intent();
				intent.setClass(getActivity(), PlanContentActivity.class);
				intent.putExtra("cbbean", caiboRmdBean);
				startActivity(intent);

			}
		});

	}
	private void doMessage(Message msg) {
		switch (msg.arg1) {
		case Constants.HTTP_OK_LASTID://返回laseid
			BaseEntity entity = (BaseEntity) msg.obj;
			if (entity != null) {
			//	lastID = entity.getLastId();
			}
			
			break;
		
		case Constants.HTTP_OK://请求成功
			List<CaiboRmdBean> caiboRecommondBeanList = (List<CaiboRmdBean>) msg.obj;
			if (caiboRecommondBeanList != null) {
				if (isxl) {
					adapter.clearDatas();
				}
				adapter.addDatas(caiboRecommondBeanList);
				datas.addAll(caiboRecommondBeanList);
			}
			break;
		case Constants.HTTP_ERROR://请求错误
			String serror = (String) msg.obj;
			showShortToast(serror);
			break;
		case Constants.HTTP_ERROR_PASER://数据解析错误
			showShortToast("数据解析错误");
			break;
		case Constants.HTTP_ERROR_NET://网络错误

			showShortToast("网络错误，请检查网络");
			break;

		default:
			break;
		}

	}

	Handler handler = new Handler(){
		public void handleMessage(Message msg) {
			dismissProgressDialog();
			pullToRefreshListView.onRefreshComplete();
			switch (msg.what) {
			case Constants.CAIBO_RMD_RUNNING:
				doMessage(msg);
				break;
				
			default:
				break;
			}

		}


	};
	/**
	 * 
	 * @ClassName:MyOnRefreshListener2
	 * @Description:TODO 下拉刷新类
	 * @author py
	 * 
	 */
	class MyOnRefreshListener2 implements OnRefreshListener2<ListView> {

		private PullToRefreshListView mPtflv;

		public MyOnRefreshListener2(PullToRefreshListView ptflv) {
			this.mPtflv = ptflv;

		}

		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
			// 下拉刷新
			String mLabel = DateUtils.formatDateTime(
					getActivity().getApplicationContext(),
					System.currentTimeMillis(), DateUtils.FORMAT_SHOW_TIME
					| DateUtils.FORMAT_SHOW_DATE
					| DateUtils.FORMAT_ABBREV_ALL);

			refreshView.getLoadingLayoutProxy().setLastUpdatedLabel(mLabel);
			lastID = "0";
			isxl = true;
			getData();
			
		}



		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
			// 上拉加载
			isxl = false;
			getData();
		}

	}

	public static Handler updeUihandler = new Handler(){


		public void handleMessage(Message msg) {
			Log.i("test", "更新ui了哦");

			if (msg.what == 1) {
				newdatas.clear();
				adapter.clearDatas();
				String name = (String) msg.obj;
				for (CaiboRmdBean bean : datas) {
					if (bean.getLotteryName().contains(name)) {
						newdatas.add(bean);
					}
				}


				adapter.addDatas(newdatas);
			}


		};
	};
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
	}


}
